package com.sqtracker.recommender.controller;

import com.sqtracker.recommender.dto.UserBehaviorDTO;
import com.sqtracker.recommender.service.UserBehaviorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user-behaviors")
@Tag(name = "用户行为接口", description = "收集和处理用户行为数据的接口")
public class UserBehaviorController {

    private final UserBehaviorService userBehaviorService;

    @Autowired
    public UserBehaviorController(UserBehaviorService userBehaviorService) {
        this.userBehaviorService = userBehaviorService;
    }

    @PostMapping
    @Operation(summary = "记录用户行为", description = "记录用户的浏览、点赞、下载等行为")
    public ResponseEntity<?> recordUserBehavior(@RequestBody UserBehaviorDTO behaviorDTO) {
        userBehaviorService.recordBehavior(behaviorDTO);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/batch")
    @Operation(summary = "批量记录用户行为", description = "批量记录多个用户行为")
    public ResponseEntity<?> recordUserBehaviorsBatch(@RequestBody UserBehaviorDTO[] behaviorsDTO) {
        userBehaviorService.recordBehaviorsBatch(behaviorsDTO);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户行为", description = "获取指定用户的行为历史")
    public ResponseEntity<?> getUserBehaviors(
            @Parameter(description = "用户ID") @PathVariable String userId,
            @Parameter(description = "返回数量限制") @RequestParam(required = false, defaultValue = "20") int limit,
            @Parameter(description = "跳过记录数") @RequestParam(required = false, defaultValue = "0") int skip) {

        return ResponseEntity.ok(userBehaviorService.getUserBehaviors(userId, limit, skip));
    }

    @GetMapping("/types")
    @Operation(summary = "获取行为类型列表", description = "获取系统支持的所有行为类型")
    public ResponseEntity<?> getBehaviorTypes() {
        return ResponseEntity.ok(userBehaviorService.getSupportedBehaviorTypes());
    }
}